package com.ruoyi.project.sdi.input.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.enums.WorkloadType;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.param.paeaParaSystem.domain.PaeaParaSystem;
import com.ruoyi.project.param.paeaParaSystem.enums.ArchiveServerType;
import com.ruoyi.project.sdi.input.model.ForceInputBatchInfoVo;
import com.ruoyi.project.sdi.input.model.ManualInputBatchInfoVo;
import com.ruoyi.project.sdi.input.model.SdiDocDefineResp;
import com.ruoyi.project.sdi.input.service.IInputService;
import com.ruoyi.project.sdi.sdiDocDefine.domain.SdiDocDefine;
import com.ruoyi.project.sdi.sdiDocDefine.service.ISdiDocDefineService;
import com.ruoyi.project.sdi.sdiPaeaArc.domain.SdiPaeaArc;
import com.ruoyi.project.sdi.sdiPaeaArc.enums.ArchivePaeaStatus;
import com.ruoyi.project.sdi.sdiPaeaArc.model.SdiPaeaArcQueryRequest;
import com.ruoyi.project.sdi.sdiPaeaArc.service.ISdiPaeaArcService;
import com.ruoyi.project.stat.paeaReportWorkload.domain.PaeaReportWorkload;
import com.ruoyi.project.stat.paeaReportWorkload.service.IStatisticsWorkloadService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.tool.DateTimeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述：
 *
 * @author 关晓强
 * @version 1.0
 * @date 2020/3/22 16:23
 */
@Controller
@RequestMapping("/sdi/input")
public class InputController extends BaseController {
    private String prefix = "sdi/input";

    @Autowired
    private ISdiDocDefineService sdiDocDefineService;

    @Autowired
    private ISdiPaeaArcService sdiPaeaArcService;

    @Autowired
    private IInputService inputService;

    @Autowired
    private IStatisticsWorkloadService statisticsWorkloadService;

    @RequiresPermissions("sdi:manualInput:view")
    @GetMapping("manualInputWelcome")
    public String manualInputWelcome()
    {
        return prefix + "/manualInputWelcome";
    }

    @RequiresPermissions("sdi:forceInput:view")
    @GetMapping("forceInputWelcome")
    public String forceInputWelcome()
    {
        return prefix + "/forceInputWelcome";
    }

    /**
     * 一般补录
     * @param mmap
     * @return
     */
    @GetMapping("/manualInput")
    @RequiresPermissions("sdi:manualInput:input")
    public String manualInput(ModelMap mmap)
    {
        User user = ShiroUtils.getSysUser();
        //文档服务器信息
        PaeaParaSystem paeaParaSystem = CommonParaTool.getPaeaParaSystemByOrgCode(user.getDeptId(), ArchiveServerType.扫描专用.getCode());
        mmap.put("paeaParaSystem",paeaParaSystem);

        mmap.put("submitUrl","/submitManualInput");
        mmap.put("chooseBatchUrl","/chooseBatch");
        mmap.put("chooseBatchDialogOpt","dialogWidth=900px; dialogHeight=600px; help=no; toolbar=no;status=no; scroll=no; resizable=no");

        //凭证类型信息
        List<SdiDocDefine> sdiDocDefines = sdiDocDefineService.list(new QueryWrapper<SdiDocDefine>().orderByAsc("code"));
        List<SdiDocDefineResp> sdiDocDefineList = sdiDocDefines.stream().map(sdiDocDefine -> {
            SdiDocDefineResp sdiDocDefineResp = new SdiDocDefineResp();
            sdiDocDefineResp.setCode(sdiDocDefine.getCode());
            sdiDocDefineResp.setName(sdiDocDefine.getName());
            sdiDocDefineResp.setMainPage(sdiDocDefine.getMainPage());
            return sdiDocDefineResp;
        }).collect(Collectors.toList());
        mmap.put("sdiDocDefineList",sdiDocDefineList);
        return prefix + "/manualInput";
    }


    /**
     * 二次补录
     * @param mmap
     * @return
     */
    @GetMapping("/forceInput")
    public String forceInput(ModelMap mmap)
    {
        User user = ShiroUtils.getSysUser();
        //文档服务器信息
        PaeaParaSystem paeaParaSystem = CommonParaTool.getPaeaParaSystemByOrgCode(user.getDeptId(), ArchiveServerType.扫描专用.getCode());
        mmap.put("paeaParaSystem",paeaParaSystem);
        mmap.put("submitUrl","/submitForceInput");
        mmap.put("chooseBatchUrl","/chooseForceInputBatch");
        mmap.put("chooseBatchDialogOpt","dialogWidth=900px; dialogHeight=600px; help=no; toolbar=no;status=no; scroll=no; resizable=no");
        return prefix + "/forceInput";
    }

    /**
     * 查看一般补录详细信息
     * @param arcId
     * @param mmap
     * @return
     */
    @RequiresPermissions("sdi:manualInput:detail")
    @GetMapping("/manualInputDetail/{arcId}")
    public String manualInputDetail(@PathVariable("arcId") String arcId, ModelMap mmap)
    {
        User user = ShiroUtils.getSysUser();
        //文档服务器信息
        PaeaParaSystem paeaParaSystem = CommonParaTool.getPaeaParaSystemByOrgCode(user.getDeptId(), ArchiveServerType.扫描专用.getCode());
        mmap.put("paeaParaSystem",paeaParaSystem);

        mmap.put("submitUrl","/submitManualInput");
        mmap.put("arcId",arcId);
        mmap.put("chooseBatchUrl","/chooseBatch");
        mmap.put("chooseBatchDialogOpt","dialogWidth=900px; dialogHeight=600px; help=no; toolbar=no;status=no; scroll=no; resizable=no");

        //凭证类型信息
        List<SdiDocDefine> sdiDocDefineList = sdiDocDefineService.list(new QueryWrapper<SdiDocDefine>().orderByAsc("code"));
        sdiDocDefineList = sdiDocDefineList.stream().map(sdiDocDefine -> {
            sdiDocDefine.setDocDefineDesc(sdiDocDefine.getCode()+"-"+sdiDocDefine.getName());
            return sdiDocDefine;
        }).collect(Collectors.toList());
        mmap.put("sdiDocDefineList",sdiDocDefineList);
        return prefix + "/manualInput";
    }

    @GetMapping("/pausedTask/{inputType}")
    public String pausedTask(@PathVariable("inputType") String inputType,ModelMap mmap) {
        mmap.put("inputType",inputType);
        return prefix + "/pausedTask";
    }

    /**
     * 人工补录下的任务列表
     */
//    @RequiresPermissions("sdi:sdiPaeaArc:list")
    @PostMapping("/listPausedTask")
    @ResponseBody
    @RequiresPermissions("sdi:manualInput:input")
    public TableDataInfo listPausedTask(SdiPaeaArcQueryRequest sdiPaeaArcQueryRequest) {
        User user = ShiroUtils.getSysUser();
        Date trDate = sdiPaeaArcQueryRequest.getTrDate();
        String orgCode = sdiPaeaArcQueryRequest.getOrgCode();
        String tellerCode = sdiPaeaArcQueryRequest.getTellerCode();
        String inputType = sdiPaeaArcQueryRequest.getInputType();
        QueryWrapper<SdiPaeaArc> qw = new QueryWrapper();
        qw.eq(trDate!=null,"tr_date",trDate)
                .eq(orgCode!=null,"org_code",orgCode)
                .eq(tellerCode!=null,"teller_code",tellerCode);
        if("manualInput".equals(inputType)) {
            qw.and(sdiPaeaArcQueryWrapper ->
                    sdiPaeaArcQueryWrapper.eq("status", ArchivePaeaStatus.DataInputing.getValue()).or()
                            .eq("status", ArchivePaeaStatus.正在重新补录795.getValue()).or()
                            .eq("status", ArchivePaeaStatus.WaitDataInput.getValue()));
        } else if("forceInput".equals(inputType)) {
            qw.eq("status", ArchivePaeaStatus.ForceInputing.getValue());
        }
        qw.eq("parent_id","0")
                .eq("locker",user.getUserId())
                .orderByDesc("locker","tr_date","status")
                .orderByAsc("org_code");
        startPage();
        //查询任务列表条件
        List<SdiPaeaArc> list = sdiPaeaArcService.list(qw);
        return getDataTable(list);
    }


    /**
     * 登记缺票
     * @param ids
     * @return
     */
    @PostMapping("/lostVoucherRegister")
    @ResponseBody
    public AjaxResult lostVoucherRegister(String ids){
        this.inputService.lostVoucherRegister(ids);
        return AjaxResult.success();
    }
    /**
     * 一般补录
     * @param arcId
     * @return
     */
    @GetMapping("/manualInputBatchInfo/{arcId}")
    @ResponseBody
    public AjaxResult manualInputBatchInfo(@PathVariable("arcId") String arcId) {
        User user = ShiroUtils.getSysUser();
        boolean isPaused = (arcId != null);
        ManualInputBatchInfoVo manualInputBatchInfoVo = inputService.obtainManualInputTask(
                arcId, user);
        return AjaxResult.success(manualInputBatchInfoVo);

    }

    /**
     * 二次补录
     * @param arcId
     * @return
     */
    @GetMapping("/forceInputBatchInfo/{arcId}")
    @ResponseBody
    public AjaxResult forceInputBatchInfo(@PathVariable("arcId") String arcId) {
        User user = ShiroUtils.getSysUser();
        boolean isPaused = (arcId != null);
        ForceInputBatchInfoVo forceInputBatchInfoVo = inputService.obtainForceInputTask(
                arcId, user);
        return AjaxResult.success(forceInputBatchInfoVo);

    }

    /**
     * 一般补录提交批次
     * @param manualInputBatchInfoVo
     * @return
     */
    @PostMapping("/submitManualInputBatch")
    @ResponseBody
    @RequiresPermissions("sdi:manualInput:input")
    public AjaxResult submitManualInputBatch(@RequestBody ManualInputBatchInfoVo manualInputBatchInfoVo) {
        boolean isSubmit = inputService.submitManualInputBatch(manualInputBatchInfoVo);
        return toAjax(isSubmit);
    }

    /**
     * 二次补录提交批次
     * @param forceInputBatchInfoVo
     * @return
     */
    @PostMapping("/submitForceInputBatch")
    @ResponseBody
    public AjaxResult submitForceInputBatch(@RequestBody ForceInputBatchInfoVo forceInputBatchInfoVo) {
        boolean isSubmit = inputService.submitForceInputBatch(forceInputBatchInfoVo);
        return toAjax(isSubmit);
    }

    /**
     * 查询当日一般补录量
     */
    @GetMapping("/manualInputWorkload")
    @ResponseBody
    public AjaxResult manualInputWorkload() {
        User user = ShiroUtils.getSysUser();
        try {
            PaeaReportWorkload workLoad = statisticsWorkloadService.getOne(new QueryWrapper<PaeaReportWorkload>()
                    .eq("work_date", DateTimeUtil.getDateFormat())
                    .eq("org_code",user.getDeptId())
                    .eq("work_type", WorkloadType.人工补录.getCode())
                    .eq("user_code",user.getUserId()));
            String responseText;
            if(workLoad!=null){
                responseText= "当日一般补录信息：补录批次数 "+workLoad.getBatchNum()+";当日补录量 "+workLoad.getWorkLoad();
            } else {
                responseText= "你今日还未进行一般补录！";
            }
            return AjaxResult.success(responseText);
        } catch (Throwable e) {
            e.printStackTrace();
            return AjaxResult.error("查询一般补录量出现异常!");
        }
    }

    /**
     * 查询当日二次补录量
     */
    @GetMapping("/forceInputWorkload")
    @ResponseBody
    public AjaxResult forceInputWorkload() {
        User user = ShiroUtils.getSysUser();
        try {
            PaeaReportWorkload workLoad = statisticsWorkloadService.getOne(new QueryWrapper<PaeaReportWorkload>()
                    .eq("work_date", DateTimeUtil.getDateFormat())
                    .eq("org_code",user.getDeptId())
                    .eq("work_type", WorkloadType.强制补录.getCode())
                    .eq("user_code",user.getUserId()));
            String responseText;
            if(workLoad!=null){
                responseText= "当日二次补录信息：补录批次数 "+workLoad.getBatchNum()+";当日补录量 "+workLoad.getWorkLoad();
            } else {
                responseText= "你今日还未进行二次补录！";
            }
            return AjaxResult.success(responseText);
        } catch (Throwable e) {
            e.printStackTrace();
            return AjaxResult.error("查询二次补录量出现异常!");
        }
    }
}
